package com.wb.base;


import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;


public class BaseFragmentAdapter {

    private BaseActivity activity;
    private RadioGroup radioGroup;
    private int flId;
    private List<BaseFragment> fragments;

    private List<RadioButton> rbs;
    private int fragmentSize;

    private int oldIndex = -1;
    private OnFragmentChangeListener onFragmentChangeListener;

    public int getOldIndex() {
        return oldIndex;
    }

    public void setOnFragmentChangeListener(OnFragmentChangeListener listener) {
        this.onFragmentChangeListener = listener;
    }

    public BaseFragmentAdapter(BaseActivity activity, List<BaseFragment> fragments, RadioGroup rg, int flId) {
        this(activity, fragments, rg, flId, 0);
    }

    public BaseFragmentAdapter(BaseActivity activity, List<BaseFragment> fragments, RadioGroup rg, int flId, int index) {
        this.activity = activity;
        this.radioGroup = rg;
        this.flId = flId;
        this.fragments = fragments;
        for (int i = 0; i < fragments.size(); i++) {
            getFragment(i);
        }


        rbs = new ArrayList<>();
        findRadioButton(radioGroup);
        fragmentSize = rbs.size();

        rbs.get(index).setChecked(true);
        initListener();
        change(index);
    }

    private void findRadioButton(ViewGroup vg) {
        int chidCount = vg.getChildCount();
        for (int i = 0; i < chidCount; i++) {
            //根据下标在radio里面去获取控件
            View v = radioGroup.getChildAt(i);

            //判断v 是不是RadioButton
            if (v instanceof RadioButton) {
                rbs.add((RadioButton) v);
            } else if (v instanceof ViewGroup) {
                findRadioButton((ViewGroup) v);
            }
        }

    }

    private void initListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                for (int i = 0; i < fragmentSize; i++) {
                    if (rbs.get(i).getId() == checkedId) {
                        //当前选中的控件是 rbs.get(i)  checkedId
                        //当前选中的第几个 i
                        change(i);
                    }
                }
            }
        });
    }


    private FragmentTransaction getFt() {
        return activity.getSupportFragmentManager().beginTransaction();
    }

    public void changeRb(int index) {
        if (rbs != null && rbs.size() - 1 >= index)
            rbs.get(index).setChecked(true);
    }


    public void change(int index) {

        if (index == oldIndex) {
            return;
        }
        BaseFragment frg = fragments.get(index);

        if (frg.isAdded()) {
            getFt().show(frg).commitAllowingStateLoss();
        } else {
            getFt().add(flId, frg, frg.getClass().getName()).commitAllowingStateLoss();
        }

        for (int i = 0; i < fragments.size(); i++) {
            if (index != i) {
                BaseFragment oldFragment = fragments.get(i);
                if (oldFragment.isAdded()) {
                    if (oldFragment.isResumed()) {
                        oldFragment.onPause();
                        oldFragment.onStop();
                    }
                    if (!oldFragment.isHidden())
                        getFt().hide(oldFragment).commitAllowingStateLoss();
                }
            }
        }

        oldIndex = index;

        if (onFragmentChangeListener != null) {
            onFragmentChangeListener.onChange(index);
        }

    }


    /**
     * Fragment 切换监听
     **/
    public static interface OnFragmentChangeListener {
        void onChange(int index);
    }


    private BaseFragment getFragment(int index) {
        BaseFragment oldFragment = (BaseFragment) activity.getSupportFragmentManager().findFragmentByTag(fragments.get(index).getClass().getName());
        BaseFragment fragment = fragments.get(index);

        if (oldFragment != null) {
            if (fragment == oldFragment) {
                oldFragment = fragment;
            } else {
                fragments.remove(index);
                fragments.add(index, oldFragment);
            }
        } else {
            oldFragment = fragments.get(index);
        }
        if (oldFragment.isAdded() && !oldFragment.isHidden()) {
            getFt().hide(oldFragment).commitAllowingStateLoss();
        }
        return oldFragment;
    }

}
